package cn.net.cfss.fgbp.provider.manage.solr;

import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.solr.core.convert.SolrConverter;
import org.springframework.data.solr.core.convert.SolrCustomConversions;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import java.util.*;

/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2020-2050</p>
 * <p>Company: cfss</p>
 *
 * @author lyg
 * @version 0.0.1
 * @date 2021/1/22 11:03
 * @other 以下修改下及其它信息
 * <p>v0.0.2,lyg,这是示例</p>
 */
public abstract class MySolrConverterBase implements SolrConverter, InitializingBean {
    private final GenericConversionService conversionService = new DefaultConversionService();
    private CustomConversions customConversions = new SolrCustomConversions(Collections.emptyList());

    public MySolrConverterBase() {
    }

    @Override
    public Collection<SolrInputDocument> write(@Nullable Iterable<?> source) {
        Assert.notNull(source, "Source must not be null!");
        List<SolrInputDocument> resultList = new ArrayList();
        Iterator var3 = source.iterator();

        while(var3.hasNext()) {
            Object bean = var3.next();
            if (bean instanceof SolrInputDocument) {
                resultList.add((SolrInputDocument)bean);
            } else {
                resultList.add(this.createAndWrite(bean));
            }
        }

        return resultList;
    }

    protected SolrInputDocument createAndWrite(Object source) {
        SolrInputDocument document = new SolrInputDocument(new String[0]);
        this.write(source, document);
        return document;
    }

    public CustomConversions getCustomConversions() {
        return this.customConversions;
    }

    protected boolean hasCustomReadTarget(Class<?> sourceType, Class<?> targetType) {
        return this.customConversions.hasCustomReadTarget(sourceType, targetType);
    }

    protected boolean hasCustomWriteTarget(Class<?> sourceType) {
        return this.customConversions.hasCustomWriteTarget(sourceType);
    }

    protected boolean hasCustomWriteTarget(Class<?> sourceType, Class<?> targetType) {
        return this.customConversions.hasCustomWriteTarget(sourceType, targetType);
    }

    protected boolean isSimpleType(Class<?> type) {
        return this.customConversions.isSimpleType(type);
    }

    protected Optional<Class<?>> getCustomWriteTargetType(Class<?> type) {
        return this.customConversions.getCustomWriteTarget(type);
    }

    protected void registerCustomConverters(GenericConversionService conversionService) {
        if (this.customConversions != null) {
            this.customConversions.registerConvertersIn(conversionService);
        }

    }

    public void setCustomConversions(@Nullable CustomConversions customConversions) {
        this.customConversions = (CustomConversions)(customConversions != null ? customConversions : new SolrCustomConversions(Collections.emptyList()));
    }

    @Override
    public GenericConversionService getConversionService() {
        return this.conversionService;
    }

    protected boolean canConvert(Class<?> sourceType, Class<?> targetType) {
        return this.conversionService.canConvert(sourceType, targetType);
    }

    @Nullable
    protected <T> T convert(Object source, Class<T> targetType) {
        if(source instanceof  String && targetType.getName().startsWith("cn.net.cfss.fgbp.spi.manage.constant"))
        {
            String str=(String)source;
            if(str.indexOf(":")>-1)
            {
                str=str.trim().split(":")[1];
            }

            return (T)Enum.valueOf((Class<Enum>) targetType, str);
        }
        return this.conversionService.convert(source, targetType);
    }

    @Override
    public void afterPropertiesSet() {
        this.registerCustomConverters(this.conversionService);
    }
}
